<?php
/**
 *Users
 * @author tan bing
 * @date 2021-08-10 14:08
 */


namespace Modules\User\Entities;


class UserCapitalLog extends BaseModel
{

    /**
     * 与模型关联的表名
     *
     * @var string
     */
    protected $table = 'user_capital_log';

    /**
     * 重定义主键
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指示是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * The storage format of the model's date columns.
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i:s';

    /**
     * 不可批量赋值的属性。
     *
     * @var array
     */
    protected $guarded = [];

    //可搜索字段
    protected $searchField = [];

    //可作为条件的字段
    protected $whereField  = ['user_id', 'type', 'operate'];

    // 新增字段
    protected $appends = ['type_text', 'operate_text', 'status_text'];

    /**
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:48
     */
    public function getTypeTextAttribute($value)
    {
        $text = [
            0 => '未知',
            1 => '余额',
            2 => '积分',
        ];

        return $text[$this->type];
    }

    /**
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:47
     */
    public function getOperateTextAttribute($value)
    {
        $text = [
            0 => '未知',
            1 => '充值',
            2 => '扣除',
            3 => '订单退款',
        ];

        return $text[$this->operate];
    }

    /**
     * @return string
     * @author tan bing
     * @date 2021-08-13 10:47
     */
    public function getStatusTextAttribute($value)
    {
        $text = [
            0 => '待审核',
            1 => '正常',
            2 => '失败',
        ];

        return $text[$this->status];
    }

    /**
     * 获取用户信息.
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author tan bing
     * @date 2021-08-13 10:56
     */
    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

}